var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-49424637-1']);
  _gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-48952420-1', 'n3ctar.com');
ga('send', 'pageview');

var modal = false;

// NAV MENU SHOW NAME ON HOVER
var navlink = $('.main-nav a[data-name]');
navlink.on({
    mouseenter: function () {
        $('#hover-show').text($(this).data('name'));
    },
    mouseleave: function () {
        $('#hover-show').empty();
    }
});


if($(window).width()>640){
    var contactLink = $('#contact');
    contactLink.on({
        mouseenter: function () {
            $('#hover-show-c').show();
        },
        mouseleave: function () {
            $('#hover-show-c').hide();
        }
    });
}
// NAVIGATION MENU OFF-CANVAS
$('#toggle-menu').on('click', function(e){
    e.preventDefault();
    $('body').toggleClass('off-canvas-on');
    $('.overlay').fadeToggle();
});
$('.overlay').on('click', function(e){
    e.preventDefault();
    $('body').toggleClass('off-canvas-on');
    $('.overlay').fadeToggle();
});

// MODAL CASOS
var tags = [];
var casoKey;
var casos;
$.get('js/casos', function(data) {
    casos = $.parseJSON(data);
});


function getModal(){
    var nextKey;
    var prevKey;
      
    
    if(casoKey+1 < selected.length){
        nextKey = casoKey+1;
    }else{
        nextKey = 0;
    }
    
    if(casoKey > 0){
        prevKey = casoKey-1;
    }else{
        prevKey = selected.length-1;
    }
    console.log(selected);
    
    var nextTitle = selected[nextKey].title;
    var nextSlug = selected[nextKey].slug;
    var prevSlug = selected[prevKey].slug;
    var prevTitle = selected[prevKey].title;
    $('#next-title strong').text(nextTitle);
    $('#prev-title strong').text(prevTitle);
    $('#next-caso').attr('data-to', nextSlug);
    $('#prev-caso').attr('data-to', prevSlug);
    $.get('caso/'+selected[casoKey].slug+'.html', function(data){
        $('.modal-content').html(data);
    });
}

var selected = [];
$('.caso').on('click', function(e){
    if($(this).hasClass('circle')){
        $('.circle').tooltipster('hide');
    }
    
    e.preventDefault();
    modal = true;
    var slug = $(this).attr('href').substring(1);
    selected = [];
    if(tags.length > 0){
        var selSlugs = [];
        $('.caso').not('.isotope-hidden').each(function(){
            selSlugs.push($(this).attr('href').substring(1));
        });
        for(var ckey in casos){
            for(var skey in selSlugs){
                if(casos[ckey].slug === selSlugs[skey]){
                    selected.push(casos[ckey]);
                }
            }
        }
    } else {
        selected = casos;
    }
    
    for (var key in selected) {
        if(selected[key].slug == slug){
            casoKey = parseFloat(key);
        }
    }
    
    getModal();
    $('body').addClass('modal-on');
    $('.modal').fadeIn();
});

$('#next-caso').on('click', function(e){
    e.preventDefault();
    if(casoKey+1 < selected.length){
        casoKey++;
    }else{
        casoKey = 0;
    }
    getModal();
});

$('#prev-caso').on('click', function(e){
    e.preventDefault();
    if(casoKey > 0){
        casoKey--;
    }else{
        casoKey = selected.length-1;
    }
    getModal();
});

$('#close-modal').on('click', function(e){
    e.preventDefault();
    modal = false;
    $('body').removeClass('modal-on');
    $('.modal').fadeOut();
});

$(document).on('keydown', function(e){
    if (modal){
        switch (e.keyCode){
            case 27:
                e.preventDefault();
                modal = false;
                $('body').removeClass('modal-on');
                $('.modal').fadeOut();
                break;
            case 37:
                e.preventDefault();
                $('#prev-caso').trigger('click');
                break;
            case 39:
                e.preventDefault();
                $('#next-caso').trigger('click');
                break;
        } 
    }
});

if($(window).width()>640){
    var nextArrow = $('#next-caso');
    nextArrow.on({
        mouseenter: function () {
            $('#next-title').show();
        },
        mouseleave: function () {
            $('#next-title').hide();
        }
    });

    var prevArrow = $('#prev-caso');
    prevArrow.on({
        mouseenter: function () {
            $('#prev-title').show();
        },
        mouseleave: function () {
            $('#prev-title').hide();
        }
    });
}

// CONTACTO
$('footer [href="#contacto"]').on('click', function(e){
    e.preventDefault();
    if($(window).width() > 640){
        setTimeout(function() {
            Foundation.libs.dropdown.toggle($('#contact'));
        }, 1);
    } else {
        $('#toggle-menu').click();
    }
});

// LOADER
$(window).load(function(){
    $('.loader').fadeOut();
    $('.has-load').fadeIn();
});